import { markerDeleted } from "@/bus";
import type { IPoi } from "@/common/MapTypes";
import { useMarkerStore } from "@/stores/useMarkerStore";

import { ResponseCode } from "@/common/Responsetypes";
import type { Ref } from "vue";
import type { AMapCustomExposed } from "@/components/amapCustom/types";
import _ from "lodash-es";

export default function (amapRef: Ref<AMapCustomExposed | null>) {
  const store = useMarkerStore();
  const { removeMarker } = store;
  // 用户删除地图上的 user marker
  uni.$on(markerDeleted, async function (poi: IPoi) {
    const clonePoi = _.cloneDeep(poi);
    const result = await removeMarker(poi);
    if (result.code === ResponseCode.PASS) {
      amapRef.value?.delMarkerFromMap(clonePoi);
    }
    uni.showToast({
      title: result.msg,
      mask: true,
      duration: 1200,
    });
  });
  return {};
}
